﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;

public class Cards : MonoBehaviour,IPointerDownHandler,IDragHandler,IPointerUpHandler
{
    public Shape m_Shape;
    private Vector3 LocalPosition;
    public static bool IsCanFallDown;
    public int ThisPlayer;
    public static GameObject CurrntCardInHandGameObject;
    // Use this for initialization
    void Start ()
	{
	    LocalPosition = transform.localPosition;
	    FindPlayer(transform);

	}

    private void FindPlayer(Transform tr)
    {
        Transform temp = tr.parent;
        if (temp.GetComponent<Player>() != null)
        {
            ThisPlayer = temp.GetComponent<Player>().PlayerID;
        }
        else
        {
            FindPlayer(temp);
        }
    }



    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("玩家："+ThisPlayer+"  拖动："+transform.name);
        if (ThisPlayer != GameManager.CurrentPlayer)
        {
            GameManager.Instance.Dialog.SetActive(true);
            GameManager.Instance.DialogText.text = "当前不是您的回合";
            return;
        }

        GameState.CurrentModeIndex = 0;
        CurrntCardInHandGameObject = gameObject;
        int kinds = 0;
        try
        {
            while (Player.ProtoMode != null)
            {
                Player.ProtoMode -= Player.ProtoMode;
            }
        }
        catch (Exception)
        {
            throw;
        }
       
        switch (m_Shape)
        {
            case Shape.Fang:
                Player.ProtoMode = StaticData.GetFangModes;
                kinds = StaticData.GetFangModes().Length;
                break;
            case Shape.UI_LLeft:
                Player.ProtoMode = StaticData.GetLLeftModes;
                kinds = StaticData.GetLLeftModes().Length;
                break;
            case Shape.UI_LRight:
                Player.ProtoMode = StaticData.GetLRightModes;
                kinds = StaticData.GetLRightModes().Length;
                break;
            case Shape.UI_T:
                Player.ProtoMode = StaticData.GetTModes;
                kinds = StaticData.GetTModes().Length;
                break;
            case Shape.UI_Tiao:
                Player.ProtoMode = StaticData.GetTiaoModes;
                kinds = StaticData.GetTiaoModes().Length;
                break;
            case Shape.UI_ZLeft:
                Player.ProtoMode = StaticData.GetZLeftModes;
                kinds = StaticData.GetZLeftModes().Length;
                break;
            case Shape.UI_ZRight:
                Player.ProtoMode = StaticData.GetZRightModes;
                kinds = StaticData.GetZRightModes().Length;
                break;
        }

        GameState.CurrentModeKinds = kinds;
    }


    public void OnDrag(PointerEventData eventData)
    {
        if (ThisPlayer != GameManager.CurrentPlayer)
            return;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (ThisPlayer != GameManager.CurrentPlayer)
            return;
    }
}
